package com.framework.loippi.swagger;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger的配置
 * 接口文档访问地址：http://localhost:8080/项目名称/swagger-ui.html
 * @author wcj
 *
 */
@Configuration // 让Spring来加载该类配置
@EnableWebMvc // 启用Mvc，非springboot框架需要引入注解@EnableWebMvc
@EnableSwagger2 // 启用Swagger2
@EnableSwaggerBootstrapUI
@ComponentScan(basePackages = {"com.framework.loippi.controller.api","com.framework.loippi.controller.swaggerapi", "com.framework.loippi.controller.queue"})
public class SwaggerConfig {


	@Value("${swagger.enable}")
    private Boolean enable;

	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				.enable(enable)
				.groupName("aicang-app")
				.apiInfo(apiInfo()).forCodeGeneration(true).select()
				// 扫描指定包中的swagger注解
				// .apis(RequestHandlerSelectors.basePackage("com.xia.controller"))
				// 扫描所有有注解的api，用这种方式更灵活
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				.paths(PathSelectors.any())
				.build();
	}
	

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("tn")
				.description("tn")
				.termsOfServiceUrl("")
				.version("1.0.0")
				.build();
	}
}
